﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DestinationOperations;

namespace Beginning
{
    public partial class frmBusesSchedules : Form
    {
        public frmBusesSchedules()
        {
            InitializeComponent();
            BusesBindControls();
            DestinationsBindControls();
            StartHoursBindControls();
        }

        private void BusesBindControls()
        {
            var buses = from b in DAO.GetAllBuses()
                        select b.BusRegNumber;

            cbBuses.DataSource = buses.ToList();
        }

        private void DestinationsBindControls()
        {
            var destinations = from d in DAO.GetAllDestinations()
                               select d.DestistionName;

            cbDestinations.DataSource = destinations.ToList();
        }

        private void StartHoursBindControls()
        {
            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = from d in DAO.GetAllDestinations()
                            where d.DestistionName == selectedDestination
                            select d.DestinationID;

            List<int> startHoursList = DAO.GetAllStartHoursForDestination(currentID.First());
            List<string> startHour = new List<string>();

            foreach (var sh in startHoursList)
            {
                startHour.Add((sh / 60).ToString() + ":" + (sh % 60).ToString());
            }

            lbStartHours.DataSource = startHour.ToList();
        }

        private void btnAttachBus_Click(object sender, EventArgs e)
        {
            BusShedule busSchedule = new BusShedule();

            string busNumber = cbBuses.SelectedItem.ToString();
            var selectedBusID = (from b in DAO.GetAllBuses()
                                 where b.BusRegNumber == busNumber
                                 select b.BusID).First();

            string selectedDestination = cbDestinations.SelectedItem.ToString();
            var currentID = from d in DAO.GetAllDestinations()
                            where d.DestistionName == selectedDestination
                            select d.DestinationID;

            var selectedStartHourID = (from s in DAO.GetAllStartHoursForDest(currentID.First())
                                       where ((s.StartHourValue / 60) + ":" + (s.StartHourValue % 60)).ToString()
                                       == lbStartHours.Text
                                       select s.StartHourID).First();

            string[] hoursAndMinutes = lbStartHours.Text.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int hours = Convert.ToInt32(hoursAndMinutes[0]);
            int minutes = Convert.ToInt32(hoursAndMinutes[1]);

            busSchedule.BusID = selectedBusID;
            busSchedule.StartHourID = selectedStartHourID;
            busSchedule.BusSheduleDate = calendar.SelectionStart;

            DAO.AttachBus(busSchedule);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }

        private void cbDestinations_SelectedIndexChanged(object sender, EventArgs e)
        {
            StartHoursBindControls();
        }
    }
}
